package com.up.upzl.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.up.upzl.base.service.impl.BaseServiceImpl;
import com.up.upzl.biz.domain.SysApp;
import com.up.upzl.biz.repository.SysAppRepository;
import com.up.upzl.biz.service.SysAppService;

@Service
public class SysAppServiceImpl extends BaseServiceImpl<SysApp, Long> implements SysAppService {
	@Autowired
	private SysAppRepository sysAppRepository;

	@Override
	public Page<SysApp> findByCondition(String appName, Pageable page) {
		return sysAppRepository.findAll(new Specification<SysApp>() {

			@Override
			public Predicate toPredicate(Root<SysApp> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new ArrayList<>();
				if (!StringUtils.isEmpty(appName)) {
					predicates.add(cb.and(cb.like(cb.lower(root.get("appName")), "%" + appName.toLowerCase() + "%")));
				}
				return cb.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		}, page);
	}
}
